package chances.epg.ui.taglib.html;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.lang.StringUtils;

import chances.epg.ui.taglib.EpgPageContextResolver;
import chances.epg.ui.taglib.layout.AbstractLayoutTag;
import chances.epg.ui.taglib.model.Border;
import chances.epg.ui.taglib.model.EpgPageContext;
import chances.epg.ui.taglib.model.LinkModel;
import chances.epg.ui.taglib.model.Rect;
import chances.epg.ui.taglib.model.Rectable;
import chances.epg.ui.taglib.model.Widget;

@SuppressWarnings("serial")
public abstract class EpgRectTag extends EpgBodyTagSupport implements Rectable {

	protected Rect rect;
	protected Rect nativeRect;
	private String rectVar;
	private String align;
	private String valign;
	private String focusClass;
	private String widget;
	private String options;
	private String forId;
	private Border paddingRect;
	private Border marginRect;

	@Override
	public int doStartTag() throws JspException {
		if(nativeRect==null){
			this.nativeRect = new Rect();
		}
		nativeRect.subduction(this.getPaddingRect());
		int result = super.doStartTag();
		this.setLinkRect();
		return result;
	}

	@Override
	public void doAfterStart() throws JspException {

		super.doAfterStart();
		if(this.isDisplay()) {
			permutate();
		}
		if (StringUtils.isNotEmpty(this.widget)) {
			EpgPageContext epgPageContext = EpgPageContextResolver.getEpgPageContext(pageContext);
			epgPageContext.addWidget(this.createWidget());
		}

	}

	public void setRect(Rect rect) {
		this.rect = rect;
	}

	public void setRectVar(String rectVar) {
		this.rectVar = rectVar;
	}

	public void setRectStyle(String rectStyle) {
		this.rect = Rect.parseStyle(rectStyle);
		this.nativeRect = new Rect();
		this.rect.copyto(nativeRect);
	}

	protected void setLinkRect() {
		Tag aTag = this.getParent();
		if (aTag != null) {
			if (LinkTag.class.isInstance(aTag)) {
				LinkTag aLinkTag = (LinkTag) aTag;
				aLinkTag.resetRect(this.getRect());
				aLinkTag.setElementId(this.id);
			}
		}
		if (StringUtils.isNotEmpty(this.focusClass)) {
			LinkTag linkTag = findLinkTag();
			if (StringUtils.isNotEmpty(this.forId)) {
	
				LinkModel model = epgPageContext.findLink(this.forId);
				if (model != null) {
					model.addEffect(this.id, String.format("css('%s')", focusClass));
				}
			} else if (linkTag != null) {
				linkTag.addEffect(this.id, String.format("css('%s')", focusClass));
			}
		}

	}

	/**
	 * 从新排列
	 */
	public void permutate() {
	
		AbstractLayoutTag aLayoutTag = (AbstractLayoutTag) findAncestorWithClass(this, AbstractLayoutTag.class);
		if (aLayoutTag != null) {
			aLayoutTag.addComponentTag(this);
			this.rect = aLayoutTag.getChildRect(this.rect);
		} else {
			this.rect = epgPageContext.getViewport().getChildRect(this.rect, true);
		}
	}

	public Widget createWidget() {
		Widget aWidget = new Widget();
		aWidget.setType(this.widget);
		aWidget.setId(this.getId());
		aWidget.setOptions(this.options);
		aWidget.setRect(this.getRect());

		return aWidget;
	}

	public void setPadding(int value) {
		paddingRect = new Border(value);
	}

	public void setMargin(int value) {
		marginRect = new Border(value);
	}

	@Override
	public void release() {
		rectVar = null;
		align = null;
		valign = null;
		focusClass = null;
		widget = null;
		options = null;
		forId = null;
		paddingRect = null;
		marginRect = null;
		this.rect = null;
		super.release();
	}

	public Rect getRect() {
		return rect;
	}

	public String getRectVar() {
		return rectVar;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getValign() {
		return valign;
	}

	public void setValign(String valign) {
		this.valign = valign;
	}

	public String getFocusClass() {
		return focusClass;
	}

	public void setFocusClass(String focusClass) {
		this.focusClass = focusClass;
	}

	public String getWidget() {
		return widget;
	}

	public void setWidget(String widget) {
		this.widget = widget;
	}

	public String getOptions() {
		return options;
	}

	public void setOptions(String options) {
		this.options = options;
	}

	public String getForId() {
		return forId;
	}

	public void setForId(String forId) {
		this.forId = forId;
	}

	public void setPaddingStyle(String value) {
		paddingRect = Border.parseStyle(value);
	}

	public void setMarginStyle(String value) {
		marginRect = Border.parseStyle(value);
	}

	public Border getPaddingRect() {
		return paddingRect;
	}

	public Border getMarginRect() {
		return marginRect;
	}
}
